<template>
	<view class="wf-box" :style="{ paddingTop: totalheight }">
		<view class="head" :style="{ paddingTop: navBarHeight }">
			<view class="head-np">
				<view class="back" @click="back()">取消</view>
				<view class="title">发动态</view>
				<view class="text" @click="publish">发布</view>
			</view>
		</view>

		<view class="main">
			<view class="text">
				<textarea id="" maxlength="150" cols="30" rows="10" placeholder="分享新鲜事..." v-model="text"></textarea>
			</view>
			<view class="box_file">
				<uni-file-picker limit="9" @select="select" title="最多选择9张图片" @delete="deleteImg"></uni-file-picker>
			</view>
			<uni-popup ref="popup" background-color="#fff">
				<view class="box theme-pop-bg">
					<view class="box_title">发布动态扣除<text>5cc</text></view>
					<view class="box_btn">
						<view @click="close()" class="theme-c-btn">取消</view>
						<view @click="issue" class="theme-k-btn">确认</view>
					</view>
				</view>
			</uni-popup>
		</view>

	</view>
</template>

<script>
	import { throttle } from '@/utils/common.js'
	import { postDynamicApi , uploadImage } from '@/api/api.js'
	import navBarMixin from '@/mixin/navBarMixin.js'
	export default {
		data() {
			return {
				text: '',
				medias: [],
				fileArray: [],
				publishFlag: true
			}
		},
		mixins: [navBarMixin],
		onLoad(option) {
		},
		methods: {
			back() {
				uni.navigateBack()
			},
			select(e) {
				var file = e.tempFilePaths
				this.fileArray = [...this.fileArray, ...file]
				console.log('this.fileArray===', this.fileArray)
			},
			
			deleteImg(e) {
				const file = e.tempFilePath
				this.fileArray = this.fileArray.filter(item => item != file)
			},
			
			// 发布
			async publish() {
				if(!this.publishFlag) return
				this.publishFlag = false
				
				// throttle(async() => {
					if(this.fileArray.length) {
						for (var i = 0; i < this.fileArray.length; i++) {
							const res = await uploadImage(this.fileArray[i])
							
							const resData = JSON.parse(res)
							
							this.medias.push(resData.data)
						}
					}
					
					const res = await postDynamicApi({
						text: this.text,
						medias: this.medias,
						seeType: 1
					})
					
					if(!res.code) {
						uni.showToast({
							title: '发布成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						},2000)
					}
				// },4000)
			}
		},
	}
</script>

<style lang="scss" scoped>
	.wf-box {
		overflow: hidden;

		.back {
			image {
				width: 20rpx;
				height: 37rpx;
			}
		}

		.head {
			position: fixed;
			top: 0;
			z-index: 1;
			width: 100%;
			background: #fff;

			.head-np {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 100%;
				height: 96rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
				position: relative;

				.title {
					font-size: 36rpx;
					font-weight: bold;
				}

				.back {
					height: 48rpx;
					line-height: 48rpx;
					position: absolute;
					left: 30rpx;
					top: 24rpx;
					font-size: 30rpx;
				}

				.text {
					width: 104rpx;
					height: 48rpx;
					position: absolute;
					right: 30rpx;
					top: 24rpx;
					line-height: 48rpx;
					text-align: center;
					background: #58A1FF;
					border-radius: 30rpx;
					font-weight: 500;
					font-size: 25rpx;
					color: #FFFFFF;
				}
			}
		}

		.main {
			margin: 40rpx 30rpx;
			// margin-top: 126rpx;
			// margin-top: 246rpx;
			background-color: white;
			border-radius: 10rpx;

			.text {
				// background-color: white;
				margin: 20rpx 0;
				padding: 20rpx;
				font-size: 30rpx;
				overflow: hidden;

				textarea {
					width: 630rpx;
					min-height: 200rpx;
					// background-color: pink;
				}
			}

			.box_file {
				min-height: 454rpx;
				padding: 30rpx;
				
				::v-deep(.icon-add) {
					&::before {
						display: none;
					}
				}
			}

			.box {
				width: 600rpx;
				height: 350rpx;
				border-radius: 30rpx;
				position: relative;
				overflow: hidden;
			}

			.box_title {
				text-align: center;
				font-size: 32rpx;
				margin-top: 80rpx;

				text {
					color: red;
				}

			}

			.box_btn {
				display: flex;
				justify-content: space-between;
				padding: 0 62rpx;
				margin-top: 82rpx;

				view {
					width: 208rpx;
					height: 73rpx;
					line-height: 73rpx;
					border-radius: 52rpx;
					text-align: center;
					font-size: 32rpx;
				}
			}

			.box_file2 {
				margin: 50rpx auto 0;
				width: 220rpx;
				height: 220rpx;
				border: 1rpx solid #eeeeee;
				border-radius: 10rpx;
				position: relative;
				display: flex;
				justify-content: center;
				align-items: center;

				.photo {
					height: 54rpx;
				}

				.upimgs {
					width: 220rpx;
					height: 220rpx;
					border-radius: 10rpx;
				}

				.del_img {

					width: 30rpx;
					height: 30rpx;
					position: absolute;
					right: -30rpx;
					top: -30rpx;
				}
			}
		}
	}
</style>